package cn.gcyl.module.biz.controller.admin.memberorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.memberorder.vo.*;
import cn.gcyl.module.biz.dal.dataobject.memberorder.MemberOrderDO;
import cn.gcyl.module.biz.service.memberorder.MemberOrderService;

@Tag(name = "管理后台 - 会员订单管理")
@RestController
@RequestMapping("/biz/member-order")
@Validated
public class MemberOrderController {

    @Resource
    private MemberOrderService memberOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建会员订单管理")
//    @PreAuthorize("@ss.hasPermission('biz:member-order:create')")
    public CommonResult<Long> createMemberOrder(@Valid @RequestBody MemberOrderSaveReqVO createReqVO) {
        return success(memberOrderService.createMemberOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员订单管理")
//    @PreAuthorize("@ss.hasPermission('biz:member-order:update')")
    public CommonResult<Boolean> updateMemberOrder(@Valid @RequestBody MemberOrderSaveReqVO updateReqVO) {
        memberOrderService.updateMemberOrder(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除会员订单管理")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:member-order:delete')")
//    public CommonResult<Boolean> deleteMemberOrder(@RequestParam("id") Long id) {
//        memberOrderService.deleteMemberOrder(id);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除会员订单管理")
//                @PreAuthorize("@ss.hasPermission('biz:member-order:delete')")
//    public CommonResult<Boolean> deleteMemberOrderList(@RequestParam("ids") List<Long> ids) {
//        memberOrderService.deleteMemberOrderListByIds(ids);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得会员订单管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:member-order:query')")
    public CommonResult<MemberOrderRespVO> getMemberOrder(@RequestParam("id") Long id) {
        MemberOrderDO memberOrder = memberOrderService.getMemberOrder(id);
        return success(BeanUtils.toBean(memberOrder, MemberOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员订单管理分页")
//    @PreAuthorize("@ss.hasPermission('biz:member-order:query')")
    public CommonResult<PageResult<MemberOrderRespVO>> getMemberOrderPage(@Valid MemberOrderPageReqVO pageReqVO) {
        PageResult<MemberOrderDO> pageResult = memberOrderService.getMemberOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员订单管理 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:member-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberOrderExcel(@Valid MemberOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberOrderDO> list = memberOrderService.getMemberOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员订单管理.xls", "数据", MemberOrderRespVO.class,
                        BeanUtils.toBean(list, MemberOrderRespVO.class));
    }

}